package mall.controller.admin;

import mall.core.jdbc.Page;
import mall.core.web.Controller;
import mall.entity.Category;
import mall.service.CategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@WebServlet("/admin/category")
public class AdminCategoryController extends Controller {

    private final CategoryService categoryService = new CategoryService();

    @Override
    protected Category getParameterForEntity(HttpServletRequest request) {
        Category category = new Category();

        String id = request.getParameter("id");
        if (id != null && !id.isEmpty()) {
            category.setId(Integer.parseInt(id));
        }

        String name = request.getParameter("name");
        if (name != null && !name.isEmpty()) {
            category.setName(name);
        }

        return category;
    }

    /**
     * 新增
     */
    public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Category category = getParameterForEntity(request);
        categoryService.add(category);
        redirect("/admin/category?method=viewList", request, response);
    }

    /**
     * 删除
     */
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Category category = getParameterForEntity(request);
        categoryService.deleteById(category.getId());
        redirect("/admin/category?method=viewList", request, response);
    }

    /**
     * 修改
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Category category = getParameterForEntity(request);
        categoryService.update(category);
        redirect("/admin/category?method=viewList", request, response);
    }

    /**
     * 查询
     */
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Page<Category> page = getParameterForPage(request);
        Category category = getParameterForEntity(request);

        categoryService.page(page, category);

        request.setAttribute("id", category.getId());
        request.setAttribute("name", category.getName());
        setPageAttribute(request, page);

        forward("/admin/category/list", request, response);
    }

    /**
     * 访问列表页面
     */
    public void viewList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Page<Category> page = new Page<>();
        categoryService.page(page, new Category());

        setPageAttribute(request, page);

        forward("/admin/category/list", request, response);
    }

    /**
     * 访问新增页面
     */
    public void viewCreate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward("/admin/category/create", request, response);
    }

    /**
     * 访问编辑页面
     */
    public void viewEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Category category = getParameterForEntity(request);
        category = categoryService.getById(category.getId());
        request.setAttribute("category", category);
        forward("/admin/category/edit", request, response);
    }
}
